package com.sgcc.ln.pmos.e_ln_home.adapter;


import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.arouter.home.HomeArouterPath;
import com.sgcc.ln.pmos.e_ln_home.bean.InformationDisclosure2Bean;

import java.util.ArrayList;
import java.util.List;

public class InforDiscloSecondStepAdapter extends GroupedRecyclerViewAdapter {

    private static int HEADER_TYPE_NO_CHILD = 1;
    private static int HEADER_TYPE_NORMAIL = 2;

    private List<InformationDisclosure2Bean.DataBean> mDatas = new ArrayList<>();

    public void addDatas(List<InformationDisclosure2Bean.DataBean> datas) {
        this.mDatas = datas;
        notifyDataSetChanged();
    }

    public InforDiscloSecondStepAdapter(Context context) {
        super(context);
    }

    public InforDiscloSecondStepAdapter(Context context, boolean useBinding) {
        super(context, useBinding);
    }

    @Override
    public int getGroupCount() {
        return mDatas.size() == 0 ? 0 : mDatas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (mDatas.get(groupPosition).getChildren() == null) {
            return 0;
        }
        return mDatas.get(groupPosition).getChildren().size();
    }

    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return false;
    }

    @Override
    public int getHeaderViewType(int groupPosition) {
        if (mDatas.get(groupPosition).getChildren() != null &&
                mDatas.get(groupPosition).getChildren().size() != 0) {
            return HEADER_TYPE_NORMAIL;
        } else {
            return HEADER_TYPE_NO_CHILD;
        }
    }

    @Override
    public int getHeaderLayout(int viewType) {
        if (viewType == HEADER_TYPE_NO_CHILD) {
            return R.layout.layout_infor_disclo_second_step_child_item;
        } else {
            return R.layout.layout_infor_disclo_second_step_header_item;
        }
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.layout_infor_disclo_second_step_child_item;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return 0;
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {
        int viewType = getHeaderViewType(groupPosition);
        if (!TextUtils.isEmpty(mDatas.get(groupPosition).getLabel())) {
            if (viewType == HEADER_TYPE_NORMAIL) {//有子项
                holder.setText(R.id.tv_second_name, mDatas.get(groupPosition).getLabel());
                holder.get(R.id.ll_header_body).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mDatas.get(groupPosition).getChildren().size() != 0) {
                            return;
                        }
                        ARouter.getInstance()
                                .build(HomeArouterPath.HOME_INFOMATIONLIST)
                                .withString("label", mDatas.get(groupPosition).getLabel())
                                .withString("infoCode", mDatas.get(groupPosition).getInfoCode())
                                .navigation();
                    }
                });

            } else {
                holder.setText(R.id.tv_child_name, mDatas.get(groupPosition).getLabel());
                holder.get(R.id.ll_child_item).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mDatas.get(groupPosition).getChildren().size() != 0) {
                            return;
                        }
                        String _ArouterPath = "";
                        if (("最新信息".equals(mDatas.get(groupPosition).getLabel()) && "zxxx".equals(mDatas.get(groupPosition).getInfoCode())) ||
                                ("最热信息".equals(mDatas.get(groupPosition).getLabel()) && "zrxx".equals(mDatas.get(groupPosition).getInfoCode()))) {
                            _ArouterPath = HomeArouterPath.HOME_INFOMATIONMESSAGELIST;
                        } else {
                            _ArouterPath = HomeArouterPath.HOME_INFOMATIONLIST;
                        }

                        if ("sn_xhpl_sbjxjh_7_1".equals(mDatas.get(groupPosition).getInfoCode())) {
                            //输变电设备检修计划 sn_xhpl_sbjxjh_7_1
                            ARouter.getInstance()
                                    .build(HomeArouterPath.HOME_SBDSBJXJH)
                                    .withString("label", mDatas.get(groupPosition).getLabel())
                                    .withString("infoCode", mDatas.get(groupPosition).getInfoCode())
                                    .navigation();
                        } else if ("sn_xhpl_dlphyd_7_4".equals(mDatas.get(groupPosition).getInfoCode()) || "sn_xhpl_rqkzsfy_7_5".equals(mDatas.get(groupPosition).getInfoCode())) {
                            //调度省内电力平衡裕度 sn_xhpl_dlphyd_7_4  或者   调度可再生能源富余程度 sn_xhpl_rqkzsfy_7_5
                            ARouter.getInstance()
                                    .build(HomeArouterPath.HOME_DDSNDLPHYD)
                                    .withString("label", mDatas.get(groupPosition).getLabel())
                                    .withString("infoCode", mDatas.get(groupPosition).getInfoCode())
                                    .navigation();
                        } else if ("sn_xhpl_dqxtfhyc_7_2".equals(mDatas.get(groupPosition).getInfoCode()) || "sn_xhpl_dqxnyfdnl_7_3".equals(mDatas.get(groupPosition).getInfoCode())) {
                            //调度省系统负荷预测  sn_xhpl_dqxtfhyc_7_2  或者  调度分省可再生能源发电能力预测  sn_xhpl_dqxnyfdnl_7_3
                            ARouter.getInstance()
                                    .build(HomeArouterPath.HOME_PUBLICYC)
                                    .withString("label", mDatas.get(groupPosition).getLabel())
                                    .withString("infoCode", mDatas.get(groupPosition).getInfoCode())
                                    .navigation();
                        } else {
                            ARouter.getInstance()
                                    .build(_ArouterPath)
                                    .withString("label", mDatas.get(groupPosition).getLabel())
                                    .withString("infoCode", mDatas.get(groupPosition).getInfoCode())
                                    .navigation();
                        }


                    }
                });
            }
        }
    }

    @Override
    public void onBindChildViewHolder(BaseViewHolder holder, int groupPosition, int childPosition) {
        if (mDatas.get(groupPosition).getChildren() != null) {
            final List<InformationDisclosure2Bean.DataBean> mChild = mDatas.get(groupPosition).getChildren();
            holder.setText(R.id.tv_child_name, mChild.get(childPosition).getLabel());
            holder.get(R.id.ll_child_item).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDatas.get(groupPosition).getChildren().get(childPosition).getChildren().size() != 0) {
                        //去四级或者更多级菜单
                        ARouter.getInstance()
                                .build(HomeArouterPath.HOME_INFORDISCLOFOURSTEP)
                                .withString("label", mChild.get(childPosition).getLabel())
                                .withSerializable("data",mDatas.get(groupPosition).getChildren().get(childPosition))
                                .navigation();
                    } else {
                        if ("发电企业".equals(mDatas.get(groupPosition).getLabel()) || "电力用户".equals(mDatas.get(groupPosition).getLabel()) || "售电公司".equals(mDatas.get(groupPosition).getLabel())) {
                            ARouter.getInstance()
                                    .build(HomeArouterPath.HOME_INFOMATIONTYPELIST)
                                    .withString("label", mChild.get(childPosition).getLabel())
                                    .withString("infoCode", mChild.get(childPosition).getInfoCode())
                                    .navigation();
                        } else {
                            ARouter.getInstance()
                                    .build(HomeArouterPath.HOME_INFOMATIONLIST)
                                    .withString("label", mChild.get(childPosition).getLabel())
                                    .withString("infoCode", mChild.get(childPosition).getInfoCode())
                                    .navigation();
                        }
                    }
                }
            });
        }
    }

    @Override
    public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {

    }

}
